
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ForceUpdateInfo;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:检查强制更新
 *
 * @author jerryliu
 * @since 2015年11月4日 下午7:20:23
 */
public class CheckForceUpdateTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public CheckForceUpdateResponse request(int merchantId, long appId, int channelid, int gameid,
            String packagename, String appKey) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        ResponsePackage resp = new CheckCostRecordsResponsePackage();
        CheckForceUpdateResponse respObj = new CheckForceUpdateResponse();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_FORCE_UPDATE);
        params.put("packagename", packagename);
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_FORCE_UPDATE);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class CheckCostRecordsResponsePackage extends HttpBaseResponsePackage<CheckForceUpdateResponse> {

        @Override
        protected void handleResponse(CheckForceUpdateResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "CheckForceUpdateTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_FORCE_UPDATE.equalsIgnoreCase(cmd)) {
                        JSONObject jsonObject = jsonResp.optJSONObject("data");
                        if (jsonObject != null) {
                            ForceUpdateInfo info = new ForceUpdateInfo();
                            info.setVersionCode(jsonObject.getInt("versioncode"));
                            info.setUpdate(jsonObject.getBoolean("isupdate"));
                            info.setPackageAddress(jsonObject.getString("packageaddress"));
                            info.setUpdateContent(jsonObject.getString("updatecontent"));
                            info.setBannerTitle(jsonObject.getString("bannertitle"));
                            info.setBannerUrl(jsonObject.getString("bannerurl"));
                            info.setAppName(jsonObject.getString("appname"));
                            info.setPackageName(jsonObject.getString("packagename"));
                            info.setFileSize(jsonObject.getLong("filesize"));
                            info.setLinkUrl(jsonObject.getString("linkurl"));
                            response.setForceUpdateInfo(info);
                            response.setOk(true);
                        }
                    }
                    response.setOk(true);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }

    public static class CheckForceUpdateResponse extends ResponseMessage {
        private static final long serialVersionUID = 1873946247962873723L;

        private ForceUpdateInfo forceUpdateInfo;

        public ForceUpdateInfo getForceUpdateInfo() {
            return forceUpdateInfo;
        }

        public void setForceUpdateInfo(ForceUpdateInfo forceUpdateInfo) {
            this.forceUpdateInfo = forceUpdateInfo;
        }
    }

}
